<script setup lang="ts" name="Draggable">
// 拖拽组件
import VueDraggable from 'vuedraggable'
import { usePropertyForm } from '@/components/Designer/utils'
import { any, array } from 'vue-types'
import { propTypes } from '@/utils/propTypes'
import { cloneDeep } from 'lodash-es'

// 定义属性
const props = defineProps({
  // 绑定值
  modelValue: array<any>().isRequired,
  // 空的元素：点击添加按钮时，创建元素并添加到列表；默认为空对象
  emptyItem: any<unknown>().def({}),
  // 数量限制：默认为0，表示不限制
  limit: propTypes.number.def(0)
})

// 定义事件
const emit = defineEmits(['update:modelValue'])
const { formData } = usePropertyForm(props.modelValue, emit)

// 处理添加
const handleAdd = () => formData.value.push(cloneDeep(props.emptyItem || {}))
// 处理删除
const handleDelete = (index: number) => formData.value.splice(index, 1)
</script>

<template>
  <ElText type="info" size="small"> 拖动左上角的小圆点可对其排序 </ElText>
  <VueDraggable :list="formData" :force-fallback="true" :animation="200" handle=".drag-icon" class="m-t-8px" item-key="index">
    <template #item="{ element, index }">
      <div class="mb-4px flex flex-col gap-4px border border-gray-2 border-rounded rounded border-solid p-8px">
        <!-- 操作按钮区 -->
        <div class="m--8px m-b-4px flex flex-row items-center justify-between bg-gray-1 p-8px">
          <ElTooltip content="拖动排序">
            <SvgIcon icon="ic:round-drag-indicator" class="drag-icon cursor-move" />
          </ElTooltip>
          <ElTooltip content="删除">
            <SvgIcon v-if="formData.length > 1" icon="ep:delete" class="cursor-pointer text-red-5" @click="handleDelete(index)" />
          </ElTooltip>
        </div>
        <!-- 内容区 -->
        <slot :element="element" :index="index" />
      </div>
    </template>
  </VueDraggable>
  <ElTooltip :disabled="limit < 1" :content="`最多添加${limit}个`">
    <ElButton type="primary" plain class="m-t-4px w-full" :disabled="limit > 0 && formData.length >= limit" @click="handleAdd"> <SvgIcon icon="ep:plus" /><span>添加</span> </ElButton>
  </ElTooltip>
</template>

<style scoped lang="scss"></style>
